package com.example.file.web;

import com.aliyun.com.viapi.FileUtils;
import com.aliyuncs.exceptions.ClientException;
import com.example.common.Ov.LoginOv;
import com.example.common.entity.ResponseCode;
import com.example.common.entity.Rsp;
import com.example.file.feign.MessageFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class AuthenticationImageController {

    @Autowired
    MessageFeign messageFeign;

    @RequestMapping("/authenticationImage")
    public Rsp fileUp(MultipartFile file) throws IOException, ClientException {
        String filename1=file.getOriginalFilename();
        String end=filename1.substring(filename1.lastIndexOf("."));
        //获取文件名
        String filename = UUID.randomUUID().toString()+end;
        //图片的上传路径
        String path="D:\\apache-tomcat-9.0.33\\webapps\\image";
        //创建file对象
        File f = new File(path+File.separator+filename);
        file.transferTo(f);

        String src = "http://localhost:80/image/"+filename;
        System.out.println("照片本地地址："+src);

        FileUtils fileUtils = FileUtils.getInstance("LTAI4Fz8SxbGd1NsZFk9AXpp", "KfsI9xVzQo4UxXZ6BlXoxbCiecuhZy");
        String url = fileUtils.upload(src);
        System.out.println("照片生成的oss的url地址："+url);

        LoginOv loginOv = new LoginOv();
        loginOv.setMobile(url);
        Rsp rsp = messageFeign.authentication(loginOv);
        return rsp;
    }

}
